//
//  Coordinator.swift
//  ExtremeHealth
//
//  Created by Zhongkai Li 李忠凯 on 2018/8/22.
//  Copyright © 2018 GanDongKeJi. All rights reserved.
//

import UIKit
import ESTabBarController_swift
import SwiftFontName

protocol Coordinator {
    var childCoordinators: [Coordinator] { get set }
    var navigationController: UINavigationController { get set }

    func start()
}

class MainCoordinator: Coordinator {
    var childCoordinators = [Coordinator]()
    var navigationController: UINavigationController

    let tabBarController = EHTabBarController()

    static let shared: MainCoordinator = MainCoordinator(navigationController: UINavigationController())

    private init(navigationController: UINavigationController) {
        self.navigationController = navigationController
    }

    func start() {
        navigationController.setNavigationBarHidden(true, animated: false)
        navigationController.pushViewController(tabBarController, animated: false)
    }

    func home() {
        navigationController.dismiss(animated: true)
    }

    func sleep() {
        guard let navi = tabBarController.viewControllers?.first as? UINavigationController else {
            assertionFailure("find home page navicontroller error"); return
        }
        let coord = SleepCoordinator(navigationController: navi)
        childCoordinators.append(coord)
        coord.start()
    }

    // MARK: - account
    func loginHome() {
        let coord = LoginCoordinator(mainNavigationController: navigationController)
        childCoordinators.append(coord)
        coord.start()
    }

    func register() {
        guard let coord = childCoordinators.filter({ $0 is LoginCoordinator }).first as? LoginCoordinator else {
            assertionFailure("find login coordinator error"); return
        }
        coord.register()
    }
}
